/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import io.iec.edp.caf.common.JSONSerializer;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class VoJsonDeserializer<T> extends JsonDeserializer<T> {


    /**
     * 读取字符数据
     *
     * @param p 值
     * @return 得到的字符串值
     */
    protected final String readString(JsonParser p) {
        try {
            return p.getValueAsString();
        } catch (IOException e) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2001);
        }
    }

    /**
     * 读取整型数据
     *
     * @param p 值
     * @return 得到的整数值
     */
    protected final int readInt(JsonParser p) {
        try {
            return p.getValueAsInt();
        } catch (IOException e) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2002);
        }
    }

    /**
     * 读取小数数据
     *
     * @param p 值
     * @return 得到的小数值
     */
    protected final BigDecimal readDecimal(JsonParser p) {
        try {
            return p.getDecimalValue();
//             p.getValueAsDouble();
        } catch (IOException e) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2003);
        }
    }

    /**
     * 读取布尔数据
     *
     * @param p 值
     * @return 得到的布尔值
     */
    protected final boolean readBool(JsonParser p) {
        try {
            return p.getValueAsBoolean();
        } catch (IOException e) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2004);
        }
    }

    /**
     * 读取日期数据
     *
     * @param p 值
     * @return 得到的日期值
     */
    protected final Date readDateTime(JsonParser p) {
        String strValue = null;
        try {
            strValue = p.getValueAsString();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.parse(strValue);
//
        } catch (IOException | ParseException e) {
            try {
                return strValue != null && !("".equals(strValue)) ? java.sql.Date.valueOf(strValue) : null;
            } catch (Exception e1) {
                throw new BffFrameworkException(e1, ErrorCodes.BFF_FRAMEWORK_2005, strValue);
            }

        }

    }

    protected final Date readDate(JsonParser p) {
        String strValue = null;
        try {
            strValue = p.getValueAsString();
            return strValue != null && !("".equals(strValue)) ? java.sql.Date.valueOf(strValue) : null;
        } catch (Exception e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2005, strValue);
        }
    }

    /**
     * 读取二进制数据
     *
     * @param p 值
     * @return 得到的二进制数据值
     */
    protected final byte[] readBytes(JsonParser p) {
        try {
            return p.getBinaryValue();
        } catch (IOException e) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2006);
        }

    }

    /**
     * 读取枚举数据
     *
     * @param p        值
     * @param enumType 枚舉類类型
     * @return 得到的枚举值
     */
    protected final <T extends Enum<T>> T readEnum(JsonParser p, Class<T> enumType) {
        try {
            return T.valueOf(enumType, p.getValueAsString());
        } catch (IOException e) {
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2007);
        }
    }

    /**
     * 读取关联数据
     *
     * @param value 值
     * @return 得到的关联值
     */
    protected final <T> T readAssociation(JsonParser value, Class<T> assoType) {
        try {
            return JSONSerializer.getObjectMapper().<T>readValue(value, assoType);
        } catch (IOException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2008, assoType.getName());
        }
    }


}
